﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;
using System.Runtime.InteropServices;

namespace FWMS
{
    public partial class Form_kucunguanli : Form
    {
        [DllImport("kernel32.dll")]
        public static extern IntPtr _lopen(string lpPathName, int iReadWrite);
        [DllImport("kernel32.dll")]
        public static extern void fclose(IntPtr hObject);
        [DllImport("kernel32.dll")]
        public static extern bool CloseHandle(IntPtr hObject);
        public const int OF_READWRITE = 2;
        public const int OF_SHARE_DENY_NONE = 0x40;
        public readonly IntPtr HFILE_ERROR = new IntPtr(-1);
        public Form_kucunguanli()
        {
            InitializeComponent();
        }
        //类型选择
        private static int kucunstyle;
        private void Form_kucunguanli_Load(object sender, EventArgs e)
        {
            string nowdate = DateTime.Now.ToString("yyyyMMdd");
            string date = nowdate.ToString().Substring(0, 4) + "年" +
                    nowdate.ToString().Substring(4, 2) + "月" +
                    nowdate.ToString().Substring(6, 2) + "日";
            label1.Text = nowdate.ToString().Substring(4, 2) + label1.Text;
            typename.Text = "原料";
            this.nowdate.Text = date;
            kucunstyle = 0;
            yuanliaoinit();
        }
        //改变TAB
        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            kucunstyle = this.tabControl1.SelectedIndex;
            switch (kucunstyle)
            {
                case 0: typename.Text = "原料"; //kucun_update.Visible = true; 
                    yuanliaoinit(); break;
                case 1: typename.Text = "调味料"; //kucun_update.Visible = true; 
                    tiaoweiliaoinit(); break;
                case 2: typename.Text = "辅料"; //kucun_update.Visible = true; 
                    fuliaoinit(); break;
                case 3: typename.Text = "产品"; //kucun_update.Visible = false; 
                    chanpininit(); break;
                default: break;
            }
        }
        //原料初始化
        private void yuanliaoinit()
        {
            Service.KucunguanliServie service = new FWMS.Service.KucunguanliServie();
            datagridviewadddata(service.kucunguanli_getalldate("0"));
        }
        //调味料初始化
        private void tiaoweiliaoinit()
        {
            Service.KucunguanliServie service = new FWMS.Service.KucunguanliServie();
            datagridviewadddata(service.kucunguanli_getalldate("1"));
        }
        //辅料初始化
        private void fuliaoinit()
        {
            Service.KucunguanliServie service = new FWMS.Service.KucunguanliServie();
            datagridviewadddata(service.kucunguanli_getalldate("2"));
        }
        //成品初始化
        private void chanpininit()
        {
            Service.KucunguanliServie service = new FWMS.Service.KucunguanliServie();
            datagridviewadddata(service.kucunguanli_getalldate("3"));
        }
        //接收数据并显示
        private void datagridviewadddata(object bean)
        {
            switch (kucunstyle)
            {
                case 0:
                    Bean.KucunguanliBean yuancailiaobean = (Bean.KucunguanliBean)bean;
                    //清除数据集
                    if (yuancailiaobean == null)
                    {
                        MessageBox.Show("库存中无原料", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    else
                    {
                        if (yuancailiaobean.Cailiaoresult == null)
                        {
                            MessageBox.Show("当前库存中无原料", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                    }
                    this.dataGridView1.Rows.Clear();
                    for (int j = 0; j < yuancailiaobean.Cailiaoresult.Length / 6; j++)
                    {
                        this.dataGridView1.Rows.Add();
                        this.dataGridView1.Rows[j].Cells[0].Value = yuancailiaobean.Cailiaoresult[j, 0];
                        this.dataGridView1.Rows[j].Cells[1].Value = yuancailiaobean.Cailiaoresult[j, 1];
                        this.dataGridView1.Rows[j].Cells[2].Value = yuancailiaobean.Cailiaoresult[j, 2];
                        this.dataGridView1.Rows[j].Cells[3].Value = yuancailiaobean.Cailiaoresult[j, 4];
                        this.dataGridView1.Rows[j].Cells[4].Value = yuancailiaobean.Cailiaoresult[j, 3];
                        this.dataGridView1.Rows[j].Cells[5].Value = yuancailiaobean.Cailiaoresult[j, 4];
                        this.dataGridView1.Rows[j].Cells[6].Value = yuancailiaobean.Cailiaoresult[j, 5];
                        this.dataGridView1.Rows[j].Cells[7].Value = "";
                    }
                    this.progressBar1.Visible = false;
                    COMMAND.Command.ProgressaBarStop();
                    break;
                case 1:
                    Bean.KucunguanliBean tiaoweiliaobean = (Bean.KucunguanliBean)bean;
                    //清除数据集
                    this.dataGridView2.Rows.Clear();
                    if (tiaoweiliaobean == null)
                    {
                        MessageBox.Show("当前库存中无调味料", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    else
                    {
                        if (tiaoweiliaobean.Cailiaoresult == null)
                        {
                            MessageBox.Show("当前库存中无调味料", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                    }
                    for (int j = 0; j < tiaoweiliaobean.Cailiaoresult.Length / 6; j++)
                    {
                        this.dataGridView2.Rows.Add();
                        this.dataGridView2.Rows[j].Cells[0].Value = tiaoweiliaobean.Cailiaoresult[j, 0];
                        this.dataGridView2.Rows[j].Cells[1].Value = tiaoweiliaobean.Cailiaoresult[j, 1];
                        this.dataGridView2.Rows[j].Cells[2].Value = tiaoweiliaobean.Cailiaoresult[j, 2];
                        this.dataGridView2.Rows[j].Cells[3].Value = tiaoweiliaobean.Cailiaoresult[j, 4];
                        this.dataGridView2.Rows[j].Cells[4].Value = tiaoweiliaobean.Cailiaoresult[j, 3];
                        this.dataGridView2.Rows[j].Cells[5].Value = tiaoweiliaobean.Cailiaoresult[j, 4];
                        this.dataGridView2.Rows[j].Cells[6].Value = tiaoweiliaobean.Cailiaoresult[j, 5];
                        this.dataGridView2.Rows[j].Cells[7].Value = "";
                    }
                    this.progressBar1.Visible = false;
                    COMMAND.Command.ProgressaBarStop();
                    break;
                case 2:
                    Bean.KucunguanliBean fuliaobean = (Bean.KucunguanliBean)bean;
                    //清除数据集
                    this.dataGridView3.Rows.Clear();
                    if (fuliaobean == null)
                    {
                        MessageBox.Show("当前库存中无辅料", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    else
                    {
                        if (fuliaobean.Cailiaoresult == null)
                        {
                            MessageBox.Show("当前库存中无辅料", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                    }
                    for (int j = 0; j < fuliaobean.Cailiaoresult.Length / 6; j++)
                    {
                        this.dataGridView3.Rows.Add();
                        this.dataGridView3.Rows[j].Cells[0].Value = fuliaobean.Cailiaoresult[j, 0];
                        this.dataGridView3.Rows[j].Cells[1].Value = fuliaobean.Cailiaoresult[j, 1];
                        this.dataGridView3.Rows[j].Cells[2].Value = fuliaobean.Cailiaoresult[j, 2];
                        this.dataGridView3.Rows[j].Cells[3].Value = fuliaobean.Cailiaoresult[j, 4];
                        this.dataGridView3.Rows[j].Cells[4].Value = fuliaobean.Cailiaoresult[j, 3];
                        this.dataGridView3.Rows[j].Cells[5].Value = fuliaobean.Cailiaoresult[j, 4];
                        this.dataGridView3.Rows[j].Cells[6].Value = fuliaobean.Cailiaoresult[j, 5];
                        this.dataGridView3.Rows[j].Cells[7].Value = "";
                    }
                    this.progressBar1.Visible = false;
                    COMMAND.Command.ProgressaBarStop();
                    break;
                case 3:
                    Bean.KucunguanliBean chanpinbean = (Bean.KucunguanliBean)bean;
                    //清除数据集
                    this.dataGridView4.Rows.Clear();
                    if (chanpinbean == null)
                    {
                        MessageBox.Show("当前库存中无产成品", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    else
                    {
                        if (chanpinbean.Cailiaoresult == null)
                        {
                            MessageBox.Show("当前库存中无产成品", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                    }
                    for (int j = 0; j < chanpinbean.Cailiaoresult.Length / 6; j++)
                    {
                        this.dataGridView4.Rows.Add();
                        this.dataGridView4.Rows[j].Cells[0].Value = chanpinbean.Cailiaoresult[j, 0];
                        this.dataGridView4.Rows[j].Cells[1].Value = chanpinbean.Cailiaoresult[j, 1];
                        this.dataGridView4.Rows[j].Cells[2].Value = chanpinbean.Cailiaoresult[j, 2];
                        this.dataGridView4.Rows[j].Cells[3].Value = chanpinbean.Cailiaoresult[j, 4];
                        this.dataGridView4.Rows[j].Cells[4].Value = chanpinbean.Cailiaoresult[j, 3];
                        this.dataGridView4.Rows[j].Cells[5].Value = chanpinbean.Cailiaoresult[j, 4];
                        this.dataGridView4.Rows[j].Cells[6].Value = chanpinbean.Cailiaoresult[j, 5];
                        this.dataGridView4.Rows[j].Cells[7].Value = "";
                    }
                    this.progressBar1.Visible = false;
                    COMMAND.Command.ProgressaBarStop();
                    break;
                default: break;
            }
        }
        //库存导出EXCEL
        private void importexcel_Click(object sender, EventArgs e)
        {
            switch (kucunstyle)
            {
                case 0: kucun_yuanliao_importexcel(); break;//原料库存EXCEL按钮按下
                case 1: kucun_tiaoweiliao_importexcel(); break;//调味料库存EXCEL按钮按下
                case 2: kucun_fuliao_importexcel(); break;//辅料库存EXCEL按钮按下
                case 3: kucun_chanpin_importexcel(); break;//产品库存EXCEL按钮按下
                default: break;
            }
        }
        //原材料库存导出EXCEL
        private void kucun_yuanliao_importexcel()
        {
            Bean.KucunguanliBean bean = new FWMS.Bean.KucunguanliBean();
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            string path2 = "";
            saveFileDialog1.Filter = "EXCEL files(*.xls)|*.xls";
            saveFileDialog1.FilterIndex = 1;
            saveFileDialog1.RestoreDirectory = true;
            //保存EXCEL
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                path2 = saveFileDialog1.FileName.ToString();
            }
            else
            {
                return;
            }
            saveFileDialog1.Dispose();

            string path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;

            if (File.Exists(path2))
            {
                IntPtr vHandle = _lopen(path2, OF_READWRITE | OF_SHARE_DENY_NONE);
                if (vHandle == HFILE_ERROR)
                {
                    MessageBox.Show("该文件已经打开，请关闭后重新导出！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                else
                {
                    CloseHandle(vHandle);
                    File.Delete(path2);
                }
            }
            COMMAND.CommandMicrosoftExcel importexcel = new FWMS.COMMAND.CommandMicrosoftExcel();
            importexcel.Open(path + "原料库存管理MODEL.xls");

            //入库基本信息导出
            importexcel.SetCellValue(importexcel.GetSheet("原料库存"), 1, 3, label1.Text);
            //数据集合导出
            int i = 0;
            Form_kucunguanli.CheckForIllegalCrossThreadCalls = false;
            this.progressBar1.Visible = true;
            this.progressBar1.Show();
            Application.DoEvents();
            COMMAND.Command.ProgressBarStart(this.progressBar1);
            for (; i < this.dataGridView1.Rows.Count; i++)
            {
                this.progressBar1.Value = i * 100 / this.dataGridView1.Rows.Count;
                this.progressBar1.Show();
                if (this.dataGridView1.Rows[i].Cells[0].Value != null)
                {
                    importexcel.SetCellValue(importexcel.GetSheet("原料库存"), 5 + i, 1, COMMAND.Command.toString(this.dataGridView1.Rows[i].Cells[0].Value));
                    importexcel.SetCellValue(importexcel.GetSheet("原料库存"), 5 + i, 3, COMMAND.Command.toString(this.dataGridView1.Rows[i].Cells[2].Value));
                    importexcel.SetCellValue(importexcel.GetSheet("原料库存"), 5 + i, 14, COMMAND.Command.toString(this.dataGridView1.Rows[i].Cells[5].Value));
                    importexcel.SetCellValue(importexcel.GetSheet("原料库存"), 5 + i, 15, COMMAND.Command.toString(this.dataGridView1.Rows[i].Cells[4].Value));//o
                    importexcel.SetCellValue(importexcel.GetSheet("原料库存"), 5 + i, 16, COMMAND.Command.toString(this.dataGridView1.Rows[i].Cells[5].Value));
                }
                else
                {
                    continue;
                }
            }
            importexcel.SetCellValue(importexcel.GetSheet("原料库存"), 5 + i, 1, "合计");
            importexcel.SetCellValue(importexcel.GetSheet("原料库存"), 5 + i, 14, "=sum(n5:n" + (4 + i).ToString() + ")");
            importexcel.SetCellValue(importexcel.GetSheet("原料库存"), 5 + i, 15, "=sum(o5:o" + (4 + i).ToString() + ")");
            importexcel.SetCellValue(importexcel.GetSheet("原料库存"), 5 + i, 16, "=sum(p5:p" + (4 + i).ToString() + ")");
            importexcel.setCellRange(importexcel.GetSheet("原料库存"), 5, 1, 5 + i, 27);
            i++;
            //财务部长：a  保管员：陈青p
            importexcel.SetCellValue(importexcel.GetSheet("原料库存"), 5 + i, 1, "财务部长：");
            importexcel.SetCellValue(importexcel.GetSheet("原料库存"), 5 + i, 16, "制造部部长：");
            importexcel.SetCellValue(importexcel.GetSheet("原料库存"), 5 + i, 21, "保管员：陈青");
            importexcel.SaveAs(path2);
            importexcel.Close();
            COMMAND.Command.ProgressaBarStop();
            this.progressBar1.Visible = false;
            MessageBox.Show("导出EXCEL成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }
        //调味料库存导出EXCEL
        private void kucun_tiaoweiliao_importexcel()
        {
            Bean.KucunguanliBean bean = new FWMS.Bean.KucunguanliBean();
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            string path2 = "";
            saveFileDialog1.Filter = "EXCEL files(*.xls)|*.xls";
            saveFileDialog1.FilterIndex = 1;
            saveFileDialog1.RestoreDirectory = true;
            //保存EXCEL
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                path2 = saveFileDialog1.FileName.ToString();
            }
            else
            {
                return;
            }
            saveFileDialog1.Dispose();
            string path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;

            if (File.Exists(path2))
            {
                IntPtr vHandle = _lopen(path2, OF_READWRITE | OF_SHARE_DENY_NONE);
                if (vHandle == HFILE_ERROR)
                {
                    MessageBox.Show("该文件已经打开，请关闭后重新导出！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                else
                {
                    CloseHandle(vHandle);
                    File.Delete(path2);
                }
            }
            COMMAND.CommandMicrosoftExcel importexcel = new FWMS.COMMAND.CommandMicrosoftExcel();
            importexcel.Open(path + "\\调味料库存管理MODEL.xls");
            //入库基本信息导出
            importexcel.SetCellValue(importexcel.GetSheet("调味料库存"), 1, 3, label1.Text);
            //数据集合导出
            int i = 0;
            Form_kucunguanli.CheckForIllegalCrossThreadCalls = false;
            this.progressBar1.Visible = true;
            this.progressBar1.Show();
            Application.DoEvents();
            COMMAND.Command.ProgressBarStart(this.progressBar1);
            for (; i < this.dataGridView2.Rows.Count; i++)
            {
                this.progressBar1.Value = i * 100 / this.dataGridView2.Rows.Count;
                this.progressBar1.Show();
                if (this.dataGridView2.Rows[i].Cells[0].Value != null)
                {
                    importexcel.SetCellValue(importexcel.GetSheet("调味料库存"), 5 + i, 1, COMMAND.Command.toString(this.dataGridView2.Rows[i].Cells[0].Value));
                    importexcel.SetCellValue(importexcel.GetSheet("调味料库存"), 5 + i, 2, COMMAND.Command.toString(this.dataGridView2.Rows[i].Cells[1].Value));
                    importexcel.SetCellValue(importexcel.GetSheet("调味料库存"), 5 + i, 3, COMMAND.Command.toString(this.dataGridView2.Rows[i].Cells[2].Value));
                    importexcel.SetCellValue(importexcel.GetSheet("调味料库存"), 5 + i, 15, COMMAND.Command.toString(this.dataGridView2.Rows[i].Cells[3].Value));
                    importexcel.SetCellValue(importexcel.GetSheet("调味料库存"), 5 + i, 19, COMMAND.Command.toString(this.dataGridView2.Rows[i].Cells[4].Value));
                    importexcel.SetCellValue(importexcel.GetSheet("调味料库存"), 5 + i, 20, COMMAND.Command.toString(this.dataGridView2.Rows[i].Cells[5].Value));
                    importexcel.SetCellValue(importexcel.GetSheet("调味料库存"), 5 + i, 21, COMMAND.Command.toString(this.dataGridView2.Rows[i].Cells[7].Value));
                }
                else
                {
                    continue;
                }
            }
            importexcel.SetCellValue(importexcel.GetSheet("调味料库存"), 5 + i, 1, "合计");
            importexcel.SetCellValue(importexcel.GetSheet("调味料库存"), 5 + i, 15, "=sum(o5:o" + (4 + i).ToString() + ")");
            importexcel.SetCellValue(importexcel.GetSheet("调味料库存"), 5 + i, 19, "=sum(s5:s" + (4 + i).ToString() + ")");
            importexcel.SetCellValue(importexcel.GetSheet("调味料库存"), 5 + i, 20, "=sum(t5:t" + (4 + i).ToString() + ")");
            importexcel.setCellRange(importexcel.GetSheet("调味料库存"), 5, 1, 5 + i, 21);
            i++;
            importexcel.SetCellValue(importexcel.GetSheet("调味料库存"), 5 + i, 1, "财务部长：");
            importexcel.SetCellValue(importexcel.GetSheet("调味料库存"), 5 + i, 15, "保管员：陈青");
            importexcel.SaveAs(path2);
            importexcel.Close();
            this.progressBar1.Visible = false;
            COMMAND.Command.ProgressaBarStop();
            MessageBox.Show("导出EXCEL成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }
        //辅料库存导出EXCEL
        private void kucun_fuliao_importexcel()
        {
            Bean.KucunguanliBean bean = new FWMS.Bean.KucunguanliBean();
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            string path2 = "";
            saveFileDialog1.Filter = "EXCEL files(*.xls)|*.xls";
            saveFileDialog1.FilterIndex = 1;
            saveFileDialog1.RestoreDirectory = true;
            //保存EXCEL
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                path2 = saveFileDialog1.FileName.ToString();
            }
            else
            {
                return;
            }
            saveFileDialog1.Dispose();
            string path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;

            if (File.Exists(path2))
            {
                IntPtr vHandle = _lopen(path2, OF_READWRITE | OF_SHARE_DENY_NONE);
                if (vHandle == HFILE_ERROR)
                {
                    MessageBox.Show("该文件已经打开，请关闭后重新导出！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                else
                {
                    CloseHandle(vHandle);
                    File.Delete(path2);
                }
            }
            COMMAND.CommandMicrosoftExcel importexcel = new FWMS.COMMAND.CommandMicrosoftExcel();
            importexcel.Open(path + "\\辅料库存管理MODEL.xls");
            //入库基本信息导出
            importexcel.SetCellValue(importexcel.GetSheet("辅料库存"), 1, 3, label1.Text);
            //数据集合导出
            int i = 0;
            Form_kucunguanli.CheckForIllegalCrossThreadCalls = false;
            this.progressBar1.Visible = true;
            this.progressBar1.Show();
            Application.DoEvents();
            COMMAND.Command.ProgressBarStart(this.progressBar1);
            for (; i < this.dataGridView3.Rows.Count; i++)
            {
                this.progressBar1.Value = i * 100 / this.dataGridView3.Rows.Count;
                this.progressBar1.Show();
                if (this.dataGridView3.Rows[i].Cells[0].Value != null)
                {
                    importexcel.SetCellValue(importexcel.GetSheet("辅料库存"), 5 + i, 1, COMMAND.Command.toString(this.dataGridView3.Rows[i].Cells[0].Value));
                    importexcel.SetCellValue(importexcel.GetSheet("辅料库存"), 5 + i, 2, COMMAND.Command.toString(this.dataGridView3.Rows[i].Cells[1].Value));
                    importexcel.SetCellValue(importexcel.GetSheet("辅料库存"), 5 + i, 3, COMMAND.Command.toString(this.dataGridView3.Rows[i].Cells[2].Value));
                    importexcel.SetCellValue(importexcel.GetSheet("辅料库存"), 5 + i, 15, COMMAND.Command.toString(this.dataGridView3.Rows[i].Cells[3].Value));
                    importexcel.SetCellValue(importexcel.GetSheet("辅料库存"), 5 + i, 19, COMMAND.Command.toString(this.dataGridView3.Rows[i].Cells[4].Value));
                    importexcel.SetCellValue(importexcel.GetSheet("辅料库存"), 5 + i, 20, COMMAND.Command.toString(this.dataGridView3.Rows[i].Cells[5].Value));
                    importexcel.SetCellValue(importexcel.GetSheet("辅料库存"), 5 + i, 21, COMMAND.Command.toString(this.dataGridView3.Rows[i].Cells[7].Value));
                }
                else
                {
                    continue;
                }
            }
            importexcel.SetCellValue(importexcel.GetSheet("辅料库存"), 5 + i, 1, "合计");
            importexcel.SetCellValue(importexcel.GetSheet("辅料库存"), 5 + i, 15, "=sum(o5:o" + (4 + i).ToString() + ")");
            importexcel.SetCellValue(importexcel.GetSheet("辅料库存"), 5 + i, 19, "=sum(s5:s" + (4 + i).ToString() + ")");
            importexcel.SetCellValue(importexcel.GetSheet("辅料库存"), 5 + i, 20, "=sum(t5:t" + (4 + i).ToString() + ")");
            importexcel.setCellRange(importexcel.GetSheet("辅料库存"), 5, 1, 5 + i, 21);
            i++;
            importexcel.SetCellValue(importexcel.GetSheet("辅料库存"), 5 + i, 1, "财务部长：");
            importexcel.SetCellValue(importexcel.GetSheet("辅料库存"), 5 + i, 15, "保管员：陈青");
            importexcel.SaveAs(path2);
            importexcel.Close();
            this.progressBar1.Visible = false;
            COMMAND.Command.ProgressaBarStop();
            MessageBox.Show("导出EXCEL成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }
        //产品库存导出EXCEL
        private void kucun_chanpin_importexcel()
        {
            Bean.KucunguanliBean bean = new FWMS.Bean.KucunguanliBean();
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            string path2 = "";
            saveFileDialog1.Filter = "EXCEL files(*.xls)|*.xls";
            saveFileDialog1.FilterIndex = 1;
            saveFileDialog1.RestoreDirectory = true;
            //保存EXCEL
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                path2 = saveFileDialog1.FileName.ToString();
            }
            else
            {
                return;
            }
            saveFileDialog1.Dispose();
            string path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;

            if (File.Exists(path2))
            {
                IntPtr vHandle = _lopen(path2, OF_READWRITE | OF_SHARE_DENY_NONE);
                if (vHandle == HFILE_ERROR)
                {
                    MessageBox.Show("该文件已经打开，请关闭后重新导出！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                else
                {
                    CloseHandle(vHandle);
                    File.Delete(path2);
                }
            }
            COMMAND.CommandMicrosoftExcel importexcel = new FWMS.COMMAND.CommandMicrosoftExcel();
            importexcel.Open(path + "\\成品库存管理MODEL.xls");
            //入库基本信息导出
            importexcel.SetCellValue(importexcel.GetSheet("产品库存"), 1, 3, label1.Text);
            //数据集合导出
            int i = 0;
            Form_kucunguanli.CheckForIllegalCrossThreadCalls = false;
            this.progressBar1.Visible = true;
            this.progressBar1.Show();
            Application.DoEvents();
            COMMAND.Command.ProgressBarStart(this.progressBar1);
            for (; i < this.dataGridView4.Rows.Count; i++)
            {
                this.progressBar1.Value = i * 100 / this.dataGridView4.Rows.Count;
                this.progressBar1.Show();
                if (this.dataGridView4.Rows[i].Cells[0].Value != null)
                {
                    importexcel.SetCellValue(importexcel.GetSheet("产品库存"), 5 + i, 1, COMMAND.Command.toString(this.dataGridView4.Rows[i].Cells[0].Value));
                    importexcel.SetCellValue(importexcel.GetSheet("产品库存"), 5 + i, 2, COMMAND.Command.toString(this.dataGridView4.Rows[i].Cells[1].Value));
                    importexcel.SetCellValue(importexcel.GetSheet("产品库存"), 5 + i, 4, COMMAND.Command.toString(this.dataGridView4.Rows[i].Cells[2].Value));
                    importexcel.SetCellValue(importexcel.GetSheet("产品库存"), 5 + i, 16, COMMAND.Command.toString(this.dataGridView4.Rows[i].Cells[5].Value));
                    importexcel.SetCellValue(importexcel.GetSheet("产品库存"), 5 + i, 20, COMMAND.Command.toString(this.dataGridView4.Rows[i].Cells[4].Value));
                    importexcel.SetCellValue(importexcel.GetSheet("产品库存"), 5 + i, 21, COMMAND.Command.toString(this.dataGridView4.Rows[i].Cells[5].Value));
                }
                else
                {
                    continue;
                }
            }
            importexcel.SetCellValue(importexcel.GetSheet("产品库存"), 5 + i, 1, "合计");
            importexcel.SetCellValue(importexcel.GetSheet("产品库存"), 5 + i, 16, "=sum(p5:p" + (4 + i).ToString() + ")");
            importexcel.SetCellValue(importexcel.GetSheet("产品库存"), 5 + i, 20, "=sum(t5:t" + (4 + i).ToString() + ")");
            importexcel.SetCellValue(importexcel.GetSheet("产品库存"), 5 + i, 21, "=sum(u5:u" + (4 + i).ToString() + ")");
            importexcel.setCellRange(importexcel.GetSheet("产品库存"), 5, 1, 5 + i, 22);
            i++;
            importexcel.SetCellValue(importexcel.GetSheet("产品库存"), 5 + i, 1, "财务部长：");
            importexcel.SetCellValue(importexcel.GetSheet("产品库存"), 5 + i, 16, "保管员：陈青");
            importexcel.SaveAs(path2);
            importexcel.Close();
            this.progressBar1.Visible = false;
            COMMAND.Command.ProgressaBarStop();
            MessageBox.Show("导出EXCEL成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        //更新库存数据
        private void kucun_update_Click(object sender, EventArgs e)
        {
            Bean.KucunguanliBean kucunbean = new FWMS.Bean.KucunguanliBean();
            Service.KucunguanliServie service = new FWMS.Service.KucunguanliServie();
            kucunbean = service.kucunguanli_getdatestartend(kucunstyle.ToString().Trim());
            if (kucunbean == null)
            {
                MessageBox.Show("库存中无新数据更新，当前显示已为最新数据", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                Form_kucunguanli.CheckForIllegalCrossThreadCalls = false;
                this.progressBar1.Visible = true;
                this.progressBar1.Show();
                Application.DoEvents();
                COMMAND.Command.ProgressBarStart(this.progressBar1);
                //MessageBox.Show("取出库数据");
                //修改或添加数据 加
                if ("0".Equals(kucunbean.Lastmonthsavedate))
                {
                    kucunbean.Lastmonthsavedate = "00010101";
                }
                kucunbean.Lastmonthsavedate = COMMAND.Command.addDay(COMMAND.Command.add80(kucunbean.Lastmonthsavedate.Trim()));
                if (!service.kucunguanli_getrukudate(this.progressBar1, kucunbean.Lastmonthsavedate, kucunbean.Thismonthsavedate, kucunstyle.ToString().Trim()))
                {
                    MessageBox.Show("数据更新失败，请重新更新库存", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    datagridviewadddata(service.kucunguanli_getalldate(kucunstyle.ToString().Trim()));
                    MessageBox.Show("库存中数据更新完了，当前显示已为最新数据", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    COMMAND.Command.ProgressaBarStop();
                }
            }
        }
    }
}
